package com.aj.activity.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.HorizontalScrollView;

public class SpecialHorizontalScrollView extends HorizontalScrollView{

    PostionListener postionListener = null;
    public SpecialHorizontalScrollView(Context context){
        super(context);
    }
    public SpecialHorizontalScrollView(Context context, AttributeSet attrs){
        super(context,attrs);
    }
    public SpecialHorizontalScrollView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }
    public int thresholdWidth = 100;
    
    
    @Override
    protected void onScrollChanged (int l, int t, int oldl, int oldt){
        super.onScrollChanged(l, t, oldl, oldt);
        View view = (View) this.getChildAt(getChildCount() - 1);
        int subViewWidth = view.getRight();
        int x = getScrollX();
        if(subViewWidth - x - getWidth() < thresholdWidth){
            if(postionListener!=null){
                postionListener.postionRightCallback(true);
            }
        }else if(x<thresholdWidth){
            if(postionListener!=null){
                postionListener.postionLeftCallback(true);
            }
        }else{
            if(postionListener!=null){
                postionListener.postionNormalCallback(true);
            }
        }
    }
    
    public interface PostionListener{
        public void postionRightCallback(boolean right);
        public void postionLeftCallback(boolean left);
        public void postionNormalCallback(boolean normal);
    }

    public int getThresholdWidth() {
        return thresholdWidth;
    }
    public void setThresholdWidth(int thresholdWidth) {
        this.thresholdWidth = thresholdWidth;
    }
    
    public void setPostionListener( PostionListener postionListener){
        this.postionListener = postionListener;
    }
    public void removePostionListener(){
        this.postionListener=null;
    }
}
